package work2;


public class Code {
    private boolean subTurn = true;

    public synchronized void subRun() throws InterruptedException {
        while (!subTurn) {
            wait();
        }

        for (int i = 1; i <= 3; i++) {
            System.out.println("子线程执行第 " + i + " 次");
        }
        subTurn = false;
        notifyAll();
    }

    public synchronized void mainRun() throws InterruptedException {
        while (subTurn) {
            wait();
        }
        // 主线程执行5次
        for (int i = 1; i <= 5; i++) {
            System.out.println("主线程执行第 " + i + " 次");
        }
        subTurn = true;
        notifyAll();
    }
}

